package com.hdu.dwh.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@RequiredArgsConstructor
@Data
@Component
@ConfigurationProperties(prefix = "dwh.trino")
public class TrinoQueryUtils {
    /**
     * trino服务所在地址
     */
    private String address;

    /**
     * trino服务端口
     */
    private Integer port;

    /**
     * trino默认的数据仓库
     */
    private String datawarehouse;

    /**
     * trino默认的用户名
     */
    private String username;


    
    public Connection createTrinoConnection() throws SQLException {
        // 构建Trino JDBC连接字符串
        String url = String.format("jdbc:trino://%s:%d/%s", 
                                    this.address, 
                                    this.port,
                                    this.datawarehouse);
        Properties properties = new Properties();
        properties.setProperty("user", this.username);
        properties.setProperty("SSL", "false");
        return DriverManager.getConnection(url, properties);
    }

    public Statement createTrinoStatement() throws SQLException {
        Connection connection = createTrinoConnection();
        return connection.createStatement();
    }
    
}
